<?php
/**
* SocialEngine
*
* @category   Application_Extensions
* @package    Flashgame
* @copyright 2010 YouNet
* @license    http://www.modules2buy.com/license/
* @version    $Id: AdminManageController.php 6657 2010-07-01 01:38:38Z YouNet $
* @author     ToAnh
*/

/**
* @category   Application_Extensions
* @package    Flashgame
* @copyright 2010 YouNet
* @license    http://www.socialengine.net/license/
*/
class Flashgame_AdminManageController extends Core_Controller_Action_Admin
{
    public function indexAction()
    {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
        ->getNavigation('flashgame_admin_main', array(), 'flashgame_admin_main_manage');

        $this->view->page = $page = $this->_getParam("page",1);
        $this->view->paginator = Engine_Api::_()->flashgame()->getFlashgamesPaginator(array(
        'orderby' => 'flashgame_id',
        ));
        $this->view->paginator->setItemCountPerPage(10);
        $this->view->paginator->setCurrentPageNumber($page);
        if ($this->_getParam("nopackage"))
            $this->view->nopackage = 1;
        // prepare user levels
        //$user_levels = Engine_Api::_()->flashgame()->getPackageLevels();
        /*
        foreach ($user_levels as $user_level){
        $levels_prepared[$user_level->level_id]= $user_level->getTitle();
        }
        */  
        //$this->view->user_levels = $user_levels;
    }

    public function deleteAction()
    {
        // In smoothbox
        $this->_helper->layout->setLayout('admin-simple');
        $id = $this->_getParam('id');
        $this->view->flashgame_id=$id;
        // Check post
        if( $this->getRequest()->isPost())
        {
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();


            try
            {
                $flashgame = Engine_Api::_()->getItem('flashgame', $id);
                // delete the blog entry into the database
                $flashgame->delete();
                $db->commit();
            }

            catch( Exception $e )
            {
                $db->rollBack();
                throw $e;
            }

            $this->_forward('success', 'utility', 'core', array(
            'smoothboxClose' => 10,
            'parentRefresh'=> 10,
            'messages' => array('')
            ));
        }

        // Output
        $this->renderScript('admin-manage/delete.tpl');
    }
    public function paidlevelAction()
    {

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $viewer = Engine_Api::_()->user()->getViewer();
        $flashgame = Engine_Api::_()->getItem('flashgame', $this->_getParam('flashgame_id'));
        // Only admin to approve
        if ($viewer->level_id > 1)
        {
            return $this->_forward('requireauth', 'error', 'core');
            /*
            if( $viewer->getIdentity() != $flashgame->owner_id && !$this->_helper->requireAuth()->setAuthParams($flashgame, null, 'edit')->isValid())
            {
            return $this->_forward('requireauth', 'error', 'core');
            }
            */
        }
        $table = $flashgame->getTable();
        $db = $table->getAdapter();
        $db->beginTransaction();

        try
        {
            $flashgame->level = $this->_getParam('level');
            $flashgame->save();

            $db->commit();
        }

        catch( Exception $e )
        {
            $db->rollBack();
            throw $e;
        }
        $page = $this->_getParam("page",1);
        return $this->_redirect("admin/flashgame/manage/index/page/".$page);
    }
    public function paidAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $viewer = Engine_Api::_()->user()->getViewer();
        $flashgame = Engine_Api::_()->getItem('flashgame', $this->_getParam('flashgame_id'));
        // Only admin to approve
        if ($viewer->level_id > 1)
        {
            return $this->_forward('requireauth', 'error', 'core');
            /*
            if( $viewer->getIdentity() != $flashgame->owner_id && !$this->_helper->requireAuth()->setAuthParams($flashgame, null, 'edit')->isValid())
            {
            return $this->_forward('requireauth', 'error', 'core');
            }
            */
        }
        $page = $this->_getParam("page",1);
        if ( count(Engine_Api::_()->flashgame()->getPackages()) < 1 )
            return $this->_redirect("admin/games/manage/". $page  . "/nopackage/1");
            //return $this->_redirect("admin/flashgame/package/create");

        $table = $flashgame->getTable();
        $db = $table->getAdapter();
        $db->beginTransaction();

        try
        {
            $flashgame->paid = $this->_getParam('paid');
            $flashgame->save();

            $db->commit();
        }

        catch( Exception $e )
        {
            $db->rollBack();
            throw $e;
        }
        return $this->_redirect("admin/games/manage/index/page/".$page);
    }
    public function deleteselectedAction()
    {
        $this->view->ids = $ids = $this->_getParam('ids', null);
        $confirm = $this->_getParam('confirm', false);
        $this->view->count = count(explode(",", $ids));
        // Save values
        if( $this->getRequest()->isPost() && $confirm == true )
        {
            $ids_array = explode(",", $ids);
            foreach( $ids_array as $id ){
                $flashgame = Engine_Api::_()->getItem('flashgame', $id);
                if( $flashgame ) {	
                	$flashgame->delete();
                }
                //else {print_r('asd');die;} 
            }
            $this->_helper->redirector->gotoRoute(array('action' => 'index'));
    }
  }
}